package Controller;

import Dao.UserWechatDao;
import Model.GeneralUser;
import Model.Staff;
import Model.UserWechat;
import Model.permission.Permission;
import Service.RoleService;
import Service.UserWechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.permission.ConstantsUtils;
import utils.permission.IStatusMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user_wechat")
public class UserWechatController {

    @Autowired
    private UserWechatService userWechatService;

    @Autowired
    private RoleService roleService;

    /**
     * 护工/老人插入微信id标识
     * @param request
     * @param response
     * @param session
     * @return
     */
    @RequestMapping(value = "/addUser")
    @ResponseBody
    public int addUser(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        System.out.println("[ Service:护工/老人插入微信id标识！]");

        int status = -1;
        UserWechat newUser = null;
        UserWechat user = new UserWechat();

        user.setOpen_id(request.getParameter("open_id"));

        String name = request.getParameter("name");
        user.setName(name);

        String id_number = request.getParameter("id_number");
        user.setId_number(id_number);

        Integer role_id = Integer.parseInt(request.getParameter("role_id"));
        user.setRole_id(role_id);

        try {
            if(role_id == 3){   //护工
                newUser = userWechatService.fingHuByIdNumber(id_number,name);
                if(newUser != null){
                    userWechatService.updateHu(user);
                    status = 0;
                }
            }else if(role_id == 4){  //老人
                newUser = userWechatService.fingLaoByIdNumber(id_number,name);
                if(newUser != null){
                    userWechatService.updateLao(user);
                    status = 0;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return status;
    }

    /**
     * 微信小程序后续免登录验证
     * @param request
     * @param response
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectUserByOpenID")
    @ResponseBody
    public UserWechat selectUserByOpenID(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        System.out.println("[ Service: 微信小程序后续免登录验证]");
        boolean suc = true;
        UserWechat newUser = null;
        try {
            newUser = userWechatService.fingHuByOpenID(request.getParameter("open_id"));
            if(newUser == null){
                newUser = userWechatService.fingLaoByOpenID(request.getParameter("open_id"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            suc = false;
        }
        if(suc){
            if(newUser != null){
                //获取用户权限列表
                try {
                    List<Permission> allPermissions;
                    allPermissions = roleService.selectPermissionOfRole(UserWechat.ROLE_ID);

                    if(allPermissions == null){
                        throw new RuntimeException("无效的用户权限");
                    }
                    //移除路劲为/的权限，根路劲不拦截
                    newUser.setPermissions(GeneralUser.UserUtil.getNoneRootPermissions(allPermissions));
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("[ Service: 权限查询失败]");
                    return null;
                }
                session.setAttribute(ConstantsUtils.SessionObj.CURRENT_USER, newUser);
//                GeneralUser user = (GeneralUser)(session.getAttribute(ConstantsUtils.SessionObj.CURRENT_USER));
//                System.out.println(user.getUser_name());
//                System.out.println(user.getPermissions());
                System.out.println("[ Service: 登" +
                        "录成功!]");
                return newUser;
            }
        }
        return null;
    }

    /**
     * 微信小程序根据open_id和role_id查找用户信息
     * @param request
     * @param response
     * @param session
     * @return
     */
    @RequestMapping(value = "/findUserInfor")
    @ResponseBody
    public UserWechat findUserInfor(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        System.out.println("[ Service: 微信小程序获取用户信息]");
        Integer role_id = Integer.parseInt(request.getParameter("role_id"));
        int status = -1;
        UserWechat newUser = null;

        try {
            if(role_id == 3){   //护工
                newUser = userWechatService.fingHuByOpenID(request.getParameter("open_id"));
            }else if(role_id == 4){  //老人
                newUser = userWechatService.fingLaoByOpenID(request.getParameter("open_id"));
            }
            status = 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(status == -1)
            return null;
        else
            return newUser;
    }

    /**
     * 查找所有用户
     */
    @RequestMapping(value = "/getUsers", method = RequestMethod.POST)
    @ResponseBody
    public List<UserWechat> getUsers() {
        List<UserWechat> userWechats = null;
        try {
            userWechats = this.userWechatService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userWechats;
    }

}
